﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class InteractionData
    {
        public Interaction GetInteraction(int interactionId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var interaction = ctx.Interactions.Where(i => i.InteractionID == interactionId);
                return interaction.FirstOrDefault();
            }
        }

        public List<Interaction> GetAllInteractions()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.Interactions.ToList();
            }
        }

        public List<Interaction> GetInteractionsForNeighbor(int neighborId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.Interactions.Where(i=>i.NeighborID == neighborId).ToList();
            }
        }

        public int AddNewInteraction(Interaction interaction)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToInteractions(interaction);
                ctx.SaveChanges();

                Interaction newInteraction = ctx.Interactions.Where(
                    i => i.InteractionDate == interaction.InteractionDate
                    && i.NeighborID == interaction.NeighborID).FirstOrDefault();

                if (newInteraction == null)
                    return -1;
                else
                    return newInteraction.InteractionID;
            }
        }

        public void UpdateInteraction(Interaction interaction)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Interaction dbDept = ctx.Interactions.First(i => i.InteractionID == interaction.InteractionID);
                ctx.ApplyCurrentValues("Interactions", interaction);
                ctx.SaveChanges();
            }
        }

        public void DeleteInteraction(int interactionId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Interaction dbDept = ctx.Interactions.First(i => i.InteractionID == interactionId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
